//--------------------------------------------------------------------------------------------------------------------
// register the functions
var newRegistery = new Array( "getAllContractsInfo", "displayAllContractsInfo" );
allJsonsRegistry = allJsonsRegistry.concat(newRegistery); 
 

//--------------------------------------------------------------------------------------------------------------------
// Display the infrmation related to the seleced contract.
function displayAllContractsInfo( allContractsInfo ) 
{
    // clear the existing table    
    $('#id_nonSettledContract').html('');
    $('#id_settledContract').html('');
	var allCInfo = allContractsInfo.allContractInfo;
	
	// count the setlled and unsettled contracts and display the table title if there is at least one
	var nbSettled = 0;
	var nbNSettled = 0;
	for( var c = 0; c < allCInfo.length; ++ c ) {
	    if( allCInfo[c].info[0].settlementStatus < 0 ) {
	        ++nbNSettled;
	    }
	    else {
	        ++nbSettled;
	    }
	}
	// add the table of the non settled contracts
	if( nbNSettled > 0 ) {
	    $('#id_nonSettledContract').append('This table shows the contract not yet settled');
	    $('#id_nonSettledContract').append('<table class="myTable" id="id_NonSettledContractTable"></table>');
	    $('#id_nonSettledContract').append('<BR>&nbsp;<BR>');
	    // add the header
	    myNSettledTable = document.getElementById('id_NonSettledContractTable');
        var headerRow = myNSettledTable.insertRow(0);
        var newCell = headerRow.insertCell(0);
        newCell.innerHTML = '<th><div class="myBold">Contract name</div></th>';
        newCell = headerRow.insertCell(1);
        newCell.innerHTML = '<th><div class="myBold">Contract Expiry</div></th>';
        newCell = headerRow.insertCell(2);
        newCell.innerHTML = '<th><div class="myBold">Last trade date</div></th>';
        newCell = headerRow.insertCell(3);
        newCell.innerHTML = '<th><div class="myBold">Last price</div></th>';
        newCell = headerRow.insertCell(4);
        newCell.innerHTML = '<th><div class="myBold">Nb bought</div></th>';
        newCell = headerRow.insertCell(5);
        newCell.innerHTML = '<th><div class="myBold">Average buying price</div></th>';
        newCell = headerRow.insertCell(6);
        newCell.innerHTML = '<th><div class="myBold">Nb sold</div></th>';
        newCell = headerRow.insertCell(7);
        newCell.innerHTML = '<th><div class="myBold">Average selling price</div></th>';
        newCell = headerRow.insertCell(8);
        newCell.innerHTML = '<th><div class="myBold">Expected P&L</div></th>';
        newCell = headerRow.insertCell(9);
        newCell.innerHTML = '<th><div class="myBold">Best case P&L</div></th>';
        newCell = headerRow.insertCell(10);
        newCell.innerHTML = '<th><div class="myBold">Worst case P&L</div></th>';
	}
	// add the table of the settled contracts
	if( nbSettled > 0 ) {
	    $("#id_nonSettledContract").append('This table shows the contract already settled');
	    $('#id_nonSettledContract').append('<table class="myTable" id="id_settledContractTable"></table>');
	    mySettledTable = document.getElementById('id_settledContractTable');
        var headerRow = mySettledTable.insertRow(0);
        var newCell = headerRow.insertCell(0);
        newCell.innerHTML = '<th><div class="myBold">Contract name</div></th>';
        newCell = headerRow.insertCell(1);
        newCell.innerHTML = '<th><div class="myBold">Contract Expiry</div></th>';
        newCell = headerRow.insertCell(2);
        newCell.innerHTML = '<th><div class="myBold">Settlement</div></th>';
        newCell = headerRow.insertCell(3);
        newCell.innerHTML = '<th><div class="myBold">Nb bought</div></th>';
        newCell = headerRow.insertCell(4);
        newCell.innerHTML = '<th><div class="myBold">Average buying price</div></th>';
        newCell = headerRow.insertCell(5);
        newCell.innerHTML = '<th><div class="myBold">Nb sold</div></th>';
        newCell = headerRow.insertCell(6);
        newCell.innerHTML = '<th><div class="myBold">Average selling price</div></th>';
        newCell = headerRow.insertCell(7);
        newCell.innerHTML = '<th><div class="myBold">realised P&L</div></th>';
	}
	
                        
	// add each contract in its corresponding table data
	var settledCounter = 0;
	var nonSettledCounter = 0;
	for( var c = 0; c < allCInfo.length; ++ c ) {
	    if( allCInfo[c].info[0].settlementStatus < 0 ) {
	        var newRow = myNSettledTable.insertRow(nonSettledCounter+1);
            var newCell = newRow.insertCell(0);
            newCell.innerHTML = allCInfo[c].info[0].name;
            newCell = newRow.insertCell(1);
            newCell.innerHTML = allCInfo[c].info[0].expiry ;
            newCell = newRow.insertCell(2);
            newCell.innerHTML = allCInfo[c].info[0].lastTradeDate;
            newCell = newRow.insertCell(3);
            newCell.innerHTML = allCInfo[c].info[0].lastTradePrice;
            newCell = newRow.insertCell(4);
            newCell.innerHTML = allCInfo[c].info[0].numberOfBoughtContracts;
            newCell = newRow.insertCell(5);
            newCell.innerHTML = allCInfo[c].info[0].averageBuyingPrice;
            newCell = newRow.insertCell(6);
            newCell.innerHTML = allCInfo[c].info[0].numberOfSoldContracts;
            newCell = newRow.insertCell(7);
            newCell.innerHTML = allCInfo[c].info[0].averageSellingPrice;
            newCell = newRow.insertCell(8);
            newCell.innerHTML = allCInfo[c].info[0].expectedPAndL;
            newCell = newRow.insertCell(9);
            newCell.innerHTML = allCInfo[c].info[0].bestCasePAndL;
            newCell = newRow.insertCell(10);
            newCell.innerHTML =  allCInfo[c].info[0].worstCasePAndL;
            ++nonSettledCounter;
	    }
	    else {
	        var settlementStr = 'Has not paid';
	        if( allCInfo[c].info[0].settlementStatus == 1 ) {
	            settlementStr = 'Has paid';
	        }
	        if( allCInfo[c].info[0].settlementStatus == 2 ) {
	            settlementStr = 'Refunded';
	        }
	        var newRow = mySettledTable.insertRow(settledCounter+1);
            var newCell = newRow.insertCell(0);
            newCell.innerHTML = allCInfo[c].info[0].name;
            newCell = newRow.insertCell(1);
            newCell.innerHTML = allCInfo[c].info[0].expiry ;
            newCell = newRow.insertCell(2);
            newCell.innerHTML = settlementStr;
            newCell = newRow.insertCell(3);
            newCell.innerHTML = allCInfo[c].info[0].numberOfBoughtContracts;
            newCell = newRow.insertCell(4);
            newCell.innerHTML = allCInfo[c].info[0].averageBuyingPrice;
            newCell = newRow.insertCell(5);
            newCell.innerHTML = allCInfo[c].info[0].numberOfSoldContracts;
            newCell = newRow.insertCell(6);
            newCell.innerHTML = allCInfo[c].info[0].averageSellingPrice;
            newCell = newRow.insertCell(7);
            newCell.innerHTML = allCInfo[c].info[0].realisedPandL;
	        ++settledCounter;
	    }
	}
}

